﻿
//======================================================================
//
//        Copyright (C) 2013    
//        All rights reserved
//
//        filename :Commons
//        description :
//
//        created by 周海军 at  2014-03-10 14:04:18
//
//======================================================================

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web.Security;
using System.Windows.Forms;
using System.Management;
using System.Xml;

namespace HsPrint.Ut
{

    /// <summary>
    /// 静态公用类，封装主要的公用方法 
    /// </summary>
    public static class Commons
    {
        #region 数据类型转换
        /// <summary>
        /// 根据布尔值来确定显示的字符串
        /// </summary>
        /// <param name="BlValue">在判断的布尔变量</param>
        /// <param name="StrTrueValue">布尔变量值为真时显示的字符串</param>
        /// <param name="StrFalseValue">布尔变量值为假时显示的字符串</param>
        /// <returns></returns>
        public static string ConvertBoolToString(bool BlValue, string StrTrueValue, string StrFalseValue)
        {
            return BlValue ? StrTrueValue : StrFalseValue;
        }

        /// <summary>
        /// 将指定的布尔值转换为字符串“是”与“否”
        /// </summary>
        /// <param name="BlValue"></param>
        /// <returns></returns>
        public static string ConvertBoolToString(bool BlValue)
        {
            return ConvertBoolToString(BlValue, "是", "否");
        }

        /// <summary>
        /// 将日期变量的值输入为字符串，当日期等于DateTime.MinValue时输入出空值
        /// </summary>
        /// <param name="DtmTime">要输入的日期值</param>
        /// <param name="StrFormat">日期格式字符串</param>
        /// <returns>被格式化后的日期字符串</returns>
        public static string ToDateTimeString(DateTime DtmTime, string StrFormat)
        {
            if (DtmTime == DateTime.MinValue)
                return string.Empty;
            return DtmTime.ToString(StrFormat);
        }

        /// <summary>
        /// 将日期时间变量的值转换为短日期字符串
        /// </summary>
        /// <param name="DtmTime">日期时间值</param>
        /// <returns></returns>
        public static string ToShortDateString(DateTime DtmTime)
        {
            if (DtmTime == DateTime.MinValue)
                return string.Empty;
            return DtmTime.ToShortDateString();
        }

        /// <summary>
        /// 把指定的object转换为string，若是null或DBNull值则返回空字符串
        /// </summary>
        /// <param name="ObValue"></param>
        /// <returns></returns>
        public static string ConvertToString(object ObValue)
        {
            if (ObValue != null && ObValue is DateTime)
                return ConvertToString(ObValue, "yyyy-MM-dd");
            return ConvertToString(ObValue, null);
        }

        /// <summary>
        /// 将指定的对象转换为字符串值
        /// </summary>
        /// <param name="ObValue">要转换的对象</param>
        /// <param name="StrFormat">格式化字符串</param>
        /// <returns>转换后的字符串</returns>
        public static string ConvertToString(object ObValue, string StrFormat)
        {
            if (ObValue == null || Convert.IsDBNull(ObValue))
                return String.Empty;
            try
            {
                if (ObValue is Int32)
                {
                    int IntTemp = (int)ObValue;
                    if (IntTemp == int.MinValue)
                        return string.Empty;
                    return IntTemp.ToString(StrFormat);
                }
                if (ObValue is double)
                {
                    double DblTemp = (double)ObValue;
                    if (DblTemp == double.MinValue) //double类型的默认为空值的值则返回空字符串
                        return string.Empty;
                    return DblTemp.ToString(StrFormat);
                }
                if (ObValue is decimal)
                {
                    decimal DecTemp = (decimal)ObValue;
                    if (DecTemp == decimal.MinValue)
                        return string.Empty;  //decimal类型的默认为空值的值则返回空字符串
                    return DecTemp.ToString(StrFormat);
                }
                if (ObValue is DateTime)
                {
                    DateTime DtmTemp = (DateTime)ObValue;
                    if (DtmTemp == DateTime.MinValue)
                        return string.Empty; //DateTime类型的默认为空值的值则返回空字符串
                    return DtmTemp.ToString(StrFormat);
                }
                return ObValue.ToString().Trim();
            }
            catch (FormatException)
            {
                throw new FormatException("数据类型转换错误，要转换的数据类型不支持指定的格式化字符串！");
            }
        }


        #endregion

        #region 将字符串值转换为指定的数据类型
        /// <summary>
        /// 将字符串值转换为int类型的数值。
        /// </summary>
        /// <param name="StrValue">String</param>
        /// <returns></returns>
        public static int ParseIntValue(string StrValue)
        {
            try
            {
                int IntValue = 0;
                if (StrValue != null && StrValue.Trim().Length > 0)
                    IntValue = int.Parse(StrValue);
                return IntValue;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 将字符串值转换为int类型的数值。
        /// </summary>
        /// <param name="ObValue">Object</param>
        /// <returns></returns>
        public static int ParseIntValue(object ObValue)
        {
            int IntValue = 0;
            if (!Convert.IsDBNull(ObValue) && ObValue != null)
                IntValue = ParseIntValue(ObValue.ToString());
            return IntValue;
        }

        /// <summary>
        /// 将字符串值转换为Int64类型的数值。
        /// </summary>
        /// <param name="StrValue">String</param>
        /// <returns></returns>
        public static Int64 ParseInt64Value(string StrValue)
        {
            try
            {
                Int64 IntValue = 0;
                if (StrValue != null && StrValue.Trim().Length > 0)
                    IntValue = Int64.Parse(StrValue);
                return IntValue;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 将字符串值转换为Int64类型的数值。
        /// </summary>
        /// <param name="ObValue">Object</param>
        /// <returns></returns>
        public static Int64 ParseInt64Value(object ObValue)
        {
            Int64 IntValue = 0;
            if (!Convert.IsDBNull(ObValue) && ObValue != null)
                IntValue = ParseInt64Value(ObValue.ToString());
            return IntValue;
        }

        /// <summary>
        /// 将字符串值转换为Boolean类型的数值。
        /// </summary>
        /// <param name="StrValue">String</param>
        /// <returns></returns>
        public static Boolean ParseBooleanValue(string StrValue)
        {
            try
            {
                Boolean BlValue = false;
                if (StrValue != null && StrValue.Trim().Length > 0)
                    BlValue = Boolean.Parse(StrValue);
                return BlValue;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 将字符串值转换为Boolean类型的数值。
        /// </summary>
        /// <param name="ObValue">Object</param>
        /// <returns></returns>
        public static Boolean ParseBooleanValue(object ObValue)
        {
            Boolean BlValue = false;
            if (!Convert.IsDBNull(ObValue) && ObValue != null)
                BlValue = ParseBooleanValue(ObValue.ToString());
            return BlValue;
        }

        /// <summary>
        /// 将字符串值转换为DateTime类型的值。
        /// </summary>
        /// <param name="StrDateTime"></param>
        /// <returns></returns>
        public static DateTime ParseDateTimeValue(string StrDateTime)
        {
            DateTime DtmTemp = DateTime.MinValue;
            try
            {
                if (StrDateTime != null && StrDateTime.Trim().Length > 0)
                    DtmTemp = DateTime.Parse(StrDateTime);
            }
            catch { }
            return DtmTemp;
        }

        /// <summary>
        /// 将字符串值转换为decimal类型的数值。
        /// </summary>
        /// <param name="StrValue"></param>
        /// <returns></returns>
        public static decimal ParseDecimalValue(string StrValue)
        {
            try
            {
                decimal DecValue = 0;
                if (StrValue != null && StrValue.Trim().Length > 0)
                    DecValue = decimal.Parse(StrValue);
                return DecValue;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 将對象转换为decimal类型的数值。
        /// </summary>
        /// <param name="ObValue"></param>
        /// <returns></returns>
        public static decimal ParseDecimalValue(object ObValue)
        {
            decimal DecValue = 0;
            try
            {
                if (!Convert.IsDBNull(ObValue) && ObValue != null)
                    DecValue = decimal.Parse(ObValue.ToString());
            }
            catch
            { }
            return DecValue;
        }

        #endregion
    }
}
